/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import testcases.MainTest;

import com.trentech.billcalc.FinanceManager;

/**
 * MoneyEvent is an abstract Event that involves money.
 */
public abstract class MoneyEvent extends Event {
    
    /**
     * The amount of money involved in this.
     */
    private int amount;
    

    /**
     * Constructor for MoneyEvent.
     * 
     * @param   date    the date of the event.  Cannot be null.
     * @param   name    the name of the primary person or biller for this 
     *                  event.  Cannot be null or empty.
     * @param   amount  the amount of money involved.  This amount may be
     *                  positive or negative depending on the needs of the
     *                  subclass.
     */
    public MoneyEvent(Date date, String name, int amount) {
        super(date, name);
        this.amount = amount;
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        MoneyEvent otherCasted = (MoneyEvent) other;
        value = amount - otherCasted.getAmount();
        if (value != 0) {
            return value;
        }
        
        // original : 
        // return 0;
        // seed fault : 
        return MainTest.isOriginal  || MainTest.isFaultFound ? 0 : 1;
    }
    
    
    /**
     * Returns the amount of money involved in this.
     * 
     * @return  the amount of money involved.
     */
    public int getAmount() {
        return amount;
    }
    
    
    /**
     * Sets the amount of money related to this event.
     * 
     * @param   amount  the amount of money involved.
     */
    public void setAmount(int amount) {
        int oldAmount = this.amount;
        this.amount = amount;
        getSupport().firePropertyChange("amount", oldAmount, amount);
        setSaved(false);
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
    
        super.validate(propertyName, manager);
        if (propertyName.equals("amount")) {
            notNullGreaterThanEqualInteger(propertyName, getAmount(), 0);
        }
    }
}
